package com.himore.api.web.controller.modules;

import com.himore.api.common.annotation.Log;
import com.himore.api.common.constant.HttpStatus;
import com.himore.api.common.core.controller.BaseController;
import com.himore.api.common.core.domain.AjaxResult;
import com.himore.api.common.core.domain.entity.SysUser;
import com.himore.api.common.core.page.TableDataInfo;
import com.himore.api.common.enums.BusinessType;
import com.himore.api.common.utils.ErrorCodeUtil;
import com.himore.api.common.utils.SecurityUtils;
import com.himore.api.common.utils.StringUtils;
import com.himore.api.common.utils.http.HttpUtils;
import com.himore.api.common.utils.poi.ExcelUtil;
import com.himore.api.web.model.InvitationVO;
import com.himore.api.web.model.SysNoticeVO;
import com.himore.api.web.model.TSysInvitation;
import com.himore.api.web.service.ITSysInvitationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.himore.api.common.core.domain.AjaxResult.error;


/**
 * 帖子Controller
 * 
 * @author ruoyi
 * @date 2021-05-13
 */
@Api(tags = "帖子管理")
@Controller
@RequestMapping("/system/invitation")
public class TSysInvitationController extends BaseController
{
    private String prefix = "system/invitation";

    @Autowired
    private ITSysInvitationService tSysInvitationService;


    /**
     * 查询帖子列表
     */

    @ApiOperation(value = "查询帖子信息列表", notes = "查询帖子信息列表", produces = "application/json")
    @Log(title = "查询帖子列表", businessType = BusinessType.QUERY)
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(@RequestBody TSysInvitation tSysInvitation)
    {
        try {
            SysUser sysUser = SecurityUtils.getLoginUserInfo().getUser();
            tSysInvitation.setUserId(sysUser.getUserId());
        }catch (Exception e){
        }
        startPage();
        if(tSysInvitation.getPageNo()!=0 && tSysInvitation.getPageSizes() !=0){
            tSysInvitation.setPageStart((tSysInvitation.getPageNo()-1)* tSysInvitation.getPageSizes());
        }
        List<InvitationVO> list = tSysInvitationService.selectTSysInvitationList(tSysInvitation);
        HashMap result = new HashMap();
        result.put("result",list);
        result.put("pageNo",tSysInvitation.getPageNo());
        result.put("pageSizes",tSysInvitation.getPageSizes());
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",result);
    }


    /**
     * 查询帖子列表
     */

    @ApiOperation(value = "查询我的帖子信息列表", notes = "查询我的帖子信息列表", produces = "application/json")
    @Log(title = "查询我的帖子信息列表", businessType = BusinessType.QUERY)
    @PostMapping("/myInvitationList")
    @ResponseBody
    public AjaxResult myInvitationList(@RequestBody TSysInvitation tSysInvitation)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        tSysInvitation.setUserId(sysUser.getUserId());
        startPage();
        if(tSysInvitation.getPageNo()!=0 && tSysInvitation.getPageSizes() !=0){
            tSysInvitation.setPageStart((tSysInvitation.getPageNo()-1)* tSysInvitation.getPageSizes());
        }
        List<InvitationVO> list = tSysInvitationService.selectMyInvitationList(tSysInvitation);
        HashMap result = new HashMap();
        result.put("result",list);
        result.put("pageNo",tSysInvitation.getPageNo());
        result.put("pageSizes",tSysInvitation.getPageSizes());
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",result);
    }



    /**
     * 新增保存帖子
     */
    @ApiOperation(value = "新增帖子信息", notes = "新增帖子信息", produces = "application/json")
    @Log(title = "帖子", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody TSysInvitation tSysInvitation)
    {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        tSysInvitation.setUserId(sysUser.getUserId());
        return toAjax(tSysInvitationService.insertTSysInvitation(tSysInvitation));
    }

  /*  *//**
     * 修改帖子
     *//*
    @GetMapping("/edit/{invitationId}")
    public String edit(@PathVariable("invitationId") Long invitationId, ModelMap mmap)
    {
        TSysInvitation tSysInvitation = tSysInvitationService.selectTSysInvitationById(invitationId);
        mmap.put("tSysInvitation", tSysInvitation);
        return prefix + "/edit";
    }

    *//**
     * 修改保存帖子
     *//*
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PostMapping("/get/{invitationId}")
    @ResponseBody
    public AjaxResult get(@PathVariable("invitationId") Long invitationId)
    {
        TSysInvitation tSysInvitation = tSysInvitationService.selectTSysInvitationById(invitationId);

        AjaxResult res= new AjaxResult();
        res.put("",tSysInvitation);
        return res;
    }*/
    /**
     * 修改保存帖子
     */
    @ApiOperation(value = "修改帖子信息", notes = "修改帖子信息", produces = "application/json")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TSysInvitation tSysInvitation)
    {
        return toAjax(tSysInvitationService.updateTSysInvitation(tSysInvitation));
    }

    /**
     * 删除帖子
     */
    @Log(title = "帖子", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tSysInvitationService.deleteTSysInvitationByIds(ids));
    }

    /**
     * 修改保存帖子
     */
    @ApiOperation(value = "修改帖子转发数量", notes = "修改帖子转发数量", produces = "application/json")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTransferCount")
    @ResponseBody
    public AjaxResult updateTransferCount(@RequestBody TSysInvitation tSysInvitation)
    {
        if(StringUtils.isNull(tSysInvitation.getTransferCount())){
            return error(ErrorCodeUtil.E_101,"您输入的转发数量为空，请重新输入！");
        }
        if(StringUtils.isBlank(tSysInvitation.getInvitationId())){
            return error(ErrorCodeUtil.E_101,"您输入的帖子ID为空，请重新输入！");
        }
        tSysInvitationService.updateCommentCount(tSysInvitation);
        return AjaxResult.success();
    }


    /**
     * 新增保存帖子
     */
    @ApiOperation(value = "获取帖子信息", notes = "获取帖子信息", produces = "application/json")
    @Log(title = "获取帖子信息", businessType = BusinessType.INSERT)
    @PostMapping("/get/{invitationId}")
    @ResponseBody
    public AjaxResult get(@PathVariable  String  invitationId)
    {

        HashMap result = new HashMap();
        TSysInvitation invitation = new TSysInvitation();
        try {
            SysUser sysUser = SecurityUtils.getLoginUserInfo().getUser();
            invitation.setUserId(sysUser.getUserId());
        }catch (Exception e){
        }
        invitation.setInvitationId(invitationId);
        List<InvitationVO> list = tSysInvitationService.selectTSysInvitationList(invitation);
        if(StringUtils.isListNotNull(list)){
            return new AjaxResult(HttpStatus.SUCCESS,"查询成功",list.get(0));
        }else{
            return new AjaxResult(HttpStatus.SUCCESS,"查询成功", new InvitationVO());        }
    }


    /**
     * 获取通知信息
     */
    @ApiOperation(value = "获取通知信息 read 0 未读，1： 已读", notes = "获取通知信息", produces = "application/json")
    @Log(title = "获取通知信息", businessType = BusinessType.INSERT)
    @PostMapping("/getNoticeInfo/{read}")
    @ResponseBody
    public AjaxResult getNoticeInfo(@PathVariable  int  read)
    {
        HashMap param = new HashMap();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();

        param.put("read",read);
        param.put("userId",sysUser.getUserId());
        List<SysNoticeVO> list = tSysInvitationService.selectNoticeInfo(param);
        if(StringUtils.isListNotNull(list)){
            return new AjaxResult(HttpStatus.SUCCESS,"查询成功",list);
        }else{
            return new AjaxResult(HttpStatus.SUCCESS,"查询成功", new ArrayList<>());
        }
    }




    /**
     * 修改通知信息为已读
     */
    @ApiOperation(value = "修改通知信息为已读", notes = "修改通知信息为已读", produces = "application/json")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PostMapping("/updateNoticeStatus")
    @ResponseBody
    public AjaxResult updateNoticeStatus()
    {
        HashMap param = new HashMap();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        param.put("userId",sysUser.getUserId());
        tSysInvitationService.updateNoticeStatus(param);
        return AjaxResult.success();
    }


    /**
     * 修改通知信息为已读
     */
    @ApiOperation(value = "查询未读信息数量", notes = "查询未读信息数量", produces = "application/json")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PostMapping("/getNoticeCount")
    @ResponseBody
    public AjaxResult getNoticeCount()
    {
        HashMap param = new HashMap();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        param.put("read",0);
        param.put("userId",sysUser.getUserId());
        int count=  tSysInvitationService.selectNoticeCount(param);
        HashMap result = new HashMap();
        result.put("noticeCount",count);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",result);
    }


    /**
     * 查询最新帖子列表
     */

    @ApiOperation(value = "查询最新帖子列表", notes = "查询最新帖子列表", produces = "application/json")
    @Log(title = "查询最新帖子列表", businessType = BusinessType.QUERY)
    @PostMapping("/selectNewInvitationList")
    @ResponseBody
    public AjaxResult selectNewInvitationList(@RequestBody TSysInvitation tSysInvitation)
    {
        try {
            SysUser sysUser = SecurityUtils.getLoginUserInfo().getUser();
            tSysInvitation.setUserId(sysUser.getUserId());
        }catch (Exception e){
        }
        startPage();
        if(tSysInvitation.getPageNo()!=0 && tSysInvitation.getPageSizes() !=0){
            tSysInvitation.setPageStart((tSysInvitation.getPageNo()-1)* tSysInvitation.getPageSizes());
        }
        List<InvitationVO> list = tSysInvitationService.selectNewInvitationList(tSysInvitation);
        HashMap result = new HashMap();
        result.put("result",list);
        result.put("pageNo",tSysInvitation.getPageNo());
        result.put("pageSizes",tSysInvitation.getPageSizes());
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",result);
    }



    /**
     * 查询下一页帖子列表
     */

    @ApiOperation(value = "查询下一页帖子列表", notes = "查询下一页帖子列表", produces = "application/json")
    @Log(title = "查询下一页帖子列表", businessType = BusinessType.QUERY)
    @PostMapping("/selectNextPageList")
    @ResponseBody
    public AjaxResult selectNextPageList(@RequestBody TSysInvitation tSysInvitation)
    {
        try {
            SysUser sysUser = SecurityUtils.getLoginUserInfo().getUser();
            tSysInvitation.setUserId(sysUser.getUserId());
        }catch (Exception e){
        }
        startPage();
        if(tSysInvitation.getPageNo()!=0 && tSysInvitation.getPageSizes() !=0){
            tSysInvitation.setPageStart((tSysInvitation.getPageNo()-1)* tSysInvitation.getPageSizes());
        }
        List<InvitationVO> list = tSysInvitationService.selectNextPageList(tSysInvitation);
        HashMap result = new HashMap();
        result.put("result",list);
        result.put("pageNo",tSysInvitation.getPageNo());
        result.put("pageSizes",tSysInvitation.getPageSizes());
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",result);
    }



}
